#!/bin/bash
stringYearForMonitorT=`date -u +%Y`
stringtimeForMonitorT=`date -u +%Y-%m-%d`
Dir_rawdata=`echo "$HOME/data/Y$stringYearForMonitorT/$stringtimeForMonitorT/mini-GWAC"`

#echo "bias dark flat focus light" | xargs -n 1 >listdatafile 


xobjectreduction (  )
{
    x60cmF.sh $FILE
}
xbiascheckandcombine (  )
{
    :
}
xdarkcheckandcombine (  )
{
    :
}
xflatcheckandcombine (  )
{
    :
}

xchecknewimage (  )
{
    ls *.fit >newlist
    linenewimage=`wc -l newlist | awk '{print($1)}'`
    if [ $linenewimage -eq 0   ]
    then

        sleep 2
        #continue
    else
        diff oldlist newlist | grep ">" | tr -d '>' | column -t  >listmatch1
        line=`cat listmatch1 | wc -l`
        if [ "$line" -ne 0  ]
        then
            echo "new image exits"
            head -1 listmatch1 >listmatch
            FILE=`cat listmatch`
            echo $FILE >>oldlist
            echo $FILE
        #    ID_ccdtype=`gethead $FILE "ccdtype"`
        #    if [  "$ID_ccdtype"x = "BIAS"x  ]
        #    then
        #       xbiascheckandcombine  #if it is a bias image
        #   elif [ "ID_ccdtype"x  = "DARK"x  ]
        #   then
        #       xdarkcheckandcombine   #if it is a dark image
        #   elif [  "ID_ccdtype"x = "FLAT"x   ]
        #   then
        #       xflatcheckandcombine  #if it is a flat image
        #   elif [ "ID_ccdtype"x = "LIGHT"x  ]
        #   then
               xobjectreduction      # if it is an object image
        #   else
        #       echo "bad ccdtype in the $FILE"
        #   fi
        fi
    fi
}

xcheckdatafile (  )
{
    #echo "xcheckdatafile"
    #   for datafilename in `cat listdatafile`
    #cd $Dir_rawdata
    echo * | xargs  -n 1 >listotnamefile
    for datafilename in `cat listotnamefile`
    do
       Ab_datafilename=`echo $Dir_rawdata"/"$datafilename`
       echo $Ab_datafilename
       cd $Ab_datafilename
       xchecknewimage
    done

}

while :
do
    cd $Dir_rawdata
    xcheckdatafile
   # xchecknewimage
done
